// order.js
// api
const $api = require('../../utils/request.js').Api;
//订单管理
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderList: "",
    items: "",//页面默认显示
    height:"30",//默认显示高度
    openId: ""//小程序唯一ID
  },
  //跳转至订单详情
  onTapJump(params) {
    let dataset = params.currentTarget.dataset
    console.log(dataset)
    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?rid=' + dataset.rid+'&orderNo='+dataset.orderno,
    })
    // if(dataset.status != '未接单'){
    //   wx.navigateTo({
    //     url: '/pages/orderDetails/orderDetails?rid=' + dataset.rid+'&orderNo='+dataset.orderno,
    //   })
    // }
  },
  //跳转至首页
  handleGoToBack(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      openId: wx.getStorageSync("openIdWy"),
    })
    if (options.orderId == "" || options.orderId == null) {
      this.getOrdersByOpenId(wx.getStorageSync("openIdWy"));
    } else {
      this.getOrdersByOpenIdAndOrderId(wx.getStorageSync("openIdWy"), options.orderId)
    }
  },
  //订单管理默认请求
  getOrdersByOpenId(openId) {
    wx.showLoading({
      title: '加载中',
    })
    $api.getOrdersByOpenId(openId).then((res) => {
      wx.hideLoading();
      if(res.length>0){
        let num,height;
        if (res.length <= 2) {
          num = res.length
          height = 30
        } else if(res.length <= 3){
          num = 3
          height = 35
        }else if(res.length <= 4){
          num = 4
          height = 55
        }else if(res.length <= 5){
          num = 5
          height = 70
        }else if(res.length <= 6){
          num = 6
          height = 85
        }else {
          num = 7
          height = 100
        }
        this.setData({
          items: num,
          height:height,
          orderList: res,
        })
      }else{
        wx.showToast({
          title: "暂无订单",
          icon: "success",
          duration: 3000
        })
      }
      
    }).catch((error) => {
      wx.hideLoading();
      console.log(error);
    });
  },
  getOrdersByOpenIdAndOrderId(openId, orderId) {
    wx.showLoading({
      title: '加载中',
    })
    $api.getOrdersByOpenIdAndOrderId(openId, orderId).then((res) => {
      wx.hideLoading();
      let num,height;
      if (res.length <= 2) {
        num = res.length
        height = 43
      } else if(res.length <= 3){
        num = 3
        height = 65
      }else if(res.length <= 4){
        num = 4
        height = 83
      }else{
        num = 5
        height = 100
      }
      this.setData({
        items: num,
        height:height,
        orderList: res,
      })
    }).catch((error) => {
      wx.hideLoading();
      console.log(error);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
     console.info(1111);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
